package app

import (
	"fmt"
	"strings"

	"git.sr.ht/~mil/mobsql/logger"
	"git.sr.ht/~mil/mobsql/utilfuncs"
)

// SourcesetStatus queries the database for the given sourceset
func SourcesetStatus(m *MDB, sourceset []int) ([]SourceStatus, error) {
	logger.Infof("Running SourcesetStatus against %d sources", len(sourceset))
	defer logger.Infof("Running SourcesetStatus against %d sources ok", len(sourceset))

	statuses := []SourceStatus{}
	if err := m.DB.Select(
		&statuses,
		fmt.Sprintf(
			`select
				mdbid, loaded, computed, tables_loaded, tables_computed, timestamp_loaded, timestamp_computed,
				url, bbox, km, subdivision, country, municipality, provider, status, name
			 from _vsources where mdbid in (%s)`,
			strings.Join(utilfuncs.ArrayWithValue(len(sourceset), "?"), ","),
		),
		utilfuncs.Anyify(sourceset)...,
	); err != nil {
		return nil, err
	} else {
		return statuses, err
	}
}
